package com.cjb.cloudsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.SysParams;
import com.cjb.cloudsystem.service.SysParamsService;
import com.cjb.cloudsystem.vo.SysCaptchaVO;
import com.cjb.cloudsystem.vo.SysParamsVO;
import com.cjb.common.common.cache.RedisCache;
import com.cjb.common.util.Common;
import com.cjb.common.util.JsonUtils;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 1.0.0 2023-08-24
 * @Package IntelliJ IDEA
 * @Class SysParamsControl
 * @Date 2023-08-24
 */
@RestController
@Tag(name = "流程定义", description = "流程定义 API")
@ApiResponses(@ApiResponse(responseCode = "200", description = "接口请求成功"))
@RequestMapping(value = {"/sys/params"})
@AllArgsConstructor
@Slf4j
public class SysParamsController {

    /*
     * 注入事物类
     */
    private final SysParamsService sysParamsService;

    /**
     * 单个保存
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @PutMapping("/save")
    @Operation(summary = "单个保存")
    public Result<Boolean> save(@RequestBody SysParams sysParams) {
        return Result.success(sysParamsService.save(sysParams));
    }

    /**
     * 批量保存
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @PostMapping("/listSave")
    @Operation(summary = "批量保存")
    public Result<Integer> listSave(@RequestBody List<SysParams> sysParams) {
        log.info("进入操作");
        return Result.success(sysParamsService.listSave(sysParams));
    }

    /**
     * 单个查询
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @GetMapping("{id}")
    @Operation(summary = "单个查询-通过ID")
    public Result<SysParams> queryById(@PathVariable("id") Long id) {
        return Result.success(sysParamsService.queryById(id));
    }

    /**
     * 分页查询
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询-通过页码")
    public Result<Page<SysParams>> queryPage(@ParameterObject @Valid SysParamsVO sysParams) {
        Page sysParamsPage = new Page<SysParams>(sysParams.getPage(),sysParams.getLimit());
        return Result.success(sysParamsService.queryPage(sysParamsPage, sysParams));
    }

    /**
     * 批量更新
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @PostMapping("/listUpdate")
    @Operation(summary = "批量更新")
    public Result<String> listUpdate(@RequestBody List<SysParams> sysParams) {
        sysParamsService.listUpdate(sysParams);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @PostMapping("/listDelete")
    @Operation(summary = "批量删除")
    public Result<String> listDelete(@RequestBody List<SysParams> sysParams) {
        sysParamsService.listDelete(sysParams);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @PostMapping("/deleteById")
    @Operation(summary = "单个删除-通过ID")
    public Result<String> deleteById(@RequestBody SysParams sysParams) {
        sysParamsService.deleteById(sysParams);
        return Result.success();
    }

    /**
     * 返回全部信息
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @PostMapping("/returnAllData")
    @Operation(summary = "返回所有信息")
    public Result<List<SysParams>> returnAllData() {
        return Result.success(sysParamsService.returnAllData());
    }

    /**
     * 验证码控制
     *
     * @Author 1.0.0 2023-08-24
     * @Value
     * @Title
     */
    @GetMapping("/captcha/enabled")
    @Operation(summary = "是否开启验证码")
    public Result<Boolean> isCaptchaEnabled() {
        //sysCaptchaService.isCaptchaEnabled()
        return Result.success(sysParamsService.isCaptchaEnabled());
    }

    /**
     * 生成验证码
     *
     * @param
     * @return
     * @author chenjinbin
     * @email 2213859123@qq.com
     * @date 2023-07-12 9:10
     */
    @GetMapping("/captcha")
    @Operation(summary = "生成验证码")
    public Result<SysCaptchaVO> captcha() {
        return Result.success(sysParamsService.generate());
    }

    /**
     * 验证码校验
     *
     * @param
     * @return
     * @author chenjinbin
     * @email 2213859123@qq.com
     * @date 2023-07-12 9:10
     */
    @PostMapping("/validateCaptcha")
    @Operation(summary = "校验验证码")
    public Result<Boolean> validateCaptcha(@RequestBody SysCaptchaVO sysCaptchaVO) {
        return Result.success(sysParamsService.validateCaptcha(sysCaptchaVO.getKey(), sysCaptchaVO.getImage()));
    }

}
